#include "timer.h"
#include <map>
#include <queue>

namespace zh
{
    class TimerManager
    {
    public:
        TimerManager()  = default;
        ~TimerManager() = default;

        int         addTimer(int delayTime, const TimerCallbacks& callback);
        bool        removeTimer(int id);
        bool        findTimer(int id);
        std::size_t getTimerCount();

        int getNextHandleTime();

        void run();
        using TimerKey = int;

    private:
        struct TimerComparator
        {
            bool operator()(const TimerSharedPtr& left, const TimerSharedPtr& right)
            {
                return left->getExpiredTime() > right->getExpiredTime();
            }
        };

        std::priority_queue<TimerSharedPtr, std::vector<TimerSharedPtr>, TimerComparator> m_timerQueue;

        std::map<TimerKey, TimerWeakPtr> m_timerHelperMap;

        std::mutex m_mutex;
    };
} // namespace zh